<!doctype html>
<title>Handles returned by setTimeout and setInterval</title>
<link rel="author" title="Alvaro Dias" href="mailto:alvarorahul@gmail.com">
<link rel="help" href="https://html.spec.whatwg.org/multipage/#dom-windowtimers-settimeout">
<link rel="help" href="https://heycam.github.io/webidl/#es-operations">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<div id="log"></div>
<script>
"use strict";

test(() => {
  const a = setTimeout(noop);
  const b = setInterval(noop);

  clearTimeout(a);
  clearInterval(b);

  assert_true(typeof a === "number");
  assert_true(typeof b === "number");

  assert_true(a > 0);
  assert_true(b > 0);

  function noop() {
    // no-op
  }
}, "setTimeout and setInterval should return numeric handles integer that is greater than zero");

async_test(t => {
  const a = setTimeout(() => {
    // shouldn't execute since we clear immediately
    assert_true(false);
  }, 50);
  clearTimeout(a);

  let c;

  const b = setTimeout(() => {
    // this callback should execute since we haven't cleared the timeout
    const d = setInterval(() => {
      clearInterval(d);
      t.done();
    }, 50);

    assert_true(d >= c + 1); // since we created the interval after the handle c
  }, 50);
  assert_equals(b, a + 1);

  c = setInterval(() => {
    // shouldn't execute since we clear immediately
    assert_true(false);
  }, 50);
  clearInterval(c);
  assert_equals(c, b + 1);

}, "setTimeout and setInterval should return numeric handles which increment");
</script>
